package com.sakura.mall.product.spuComment.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.spuComment.domain.entity.SpuCommentEntity;
import com.sakura.mall.product.spuComment.domain.request.AddSpuCommentRequest;
import com.sakura.mall.product.spuComment.domain.request.UpdateSpuCommentRequest;
import com.sakura.mall.product.spuComment.domain.request.SpuCommentPageRequest;
import com.sakura.mall.product.spuComment.domain.vo.SpuCommentVo;
import com.sakura.mall.product.spuComment.mapper.SpuCommentMapper;
import com.sakura.mall.product.spuComment.service.SpuCommentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品评价 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SpuCommentServiceImpl extends ServiceImpl<SpuCommentMapper, SpuCommentEntity> implements SpuCommentService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSpuCommentRequest request) {
        // todo 其他校验逻辑
        SpuCommentEntity spuComment = request.convert(SpuCommentEntity.class);
        this.save(spuComment);
    }

    @Override
    public Page<SpuCommentVo> queryPage(SpuCommentPageRequest request) {
        IPage<SpuCommentEntity> qp = request.toPage();
        LambdaQueryWrapper<SpuCommentEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SpuCommentVo> ret = qp.convert(entity -> entity.convert(SpuCommentVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSpuCommentRequest request) {
        SpuCommentEntity entity = request.convert(SpuCommentEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

